<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"> </el-col>
      <el-col :span="12" :xs="24">
        <el-form :model="form" :rules="rules" ref="ruleFormRef" label-width="auto" style="max-width: 600px" class="login_form">
          <h1>Hello</h1>
          <h1>硅谷甄选</h1>
          <el-form-item prop="username">
            <el-input v-model="form.username" :prefix-icon="User" />
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="form.password" type="password" :prefix-icon="Lock" show-password autocomplete="off" />
          </el-form-item>
          <el-button type="primary" @click="submitForm(form)" class="btn" :loading="loadingState">
            登录
          </el-button>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import { LoginApi } from '@/api/user'
import useUseStore from '@/store/modules/user/index'
import { useRouter, useRoute } from 'vue-router'
import { getTime } from '@/utils/time'
let $router = useRouter()
let $route = useRoute()

let useStore = useUseStore()

import { ElNotification } from 'element-plus'
// 这是检验所有表单后的结果
const ruleFormRef = ref()

let loadingState = ref(false)
const form = reactive({
  username: 'admin',
  password: '111111'
})

let submitForm = () => {
  loadingState.value = true
  let result = ruleFormRef.value.validate()
  result.catch(() => {
    return
  })
  // 登录结果:成功和失败.
  let LoginResult = useStore.userLogin(form)
  console.log($route.query.redirect)
  // 200进入then   其他进入catch
  LoginResult.then(() => {
    if ($route.query.redirect == undefined) {
      $router.replace('/')
    } else {
      $router.replace({ path: `${$route.query.redirect}` })
    }
    ElNotification({
      title: `Hi,${getTime()}`,
      message: '欢迎你回来',
      type: 'success'
    })
    loadingState.value = false
    window.location.reload()
  }).catch(error => {
    
    
    ElNotification({
      message: error.data,
      type: 'error'
    })
    loadingState.value = false
  })
}
const validateUserName = (rule: any, value: any, callback: any) => {
  if (/^\w{3,5}$/.test(value)) {
    callback()
  } else {
    callback(new Error('账号需要3-5位'))
  }
}
const validatePassWord = (rule: any, value: any, callback: any) => {
  if (/^\d{6,8}$/.test(value)) {
    callback()
  } else {
    callback(new Error('密码需要6-8位'))
  }
}

const rules = reactive({
  username: [{ validator: validateUserName, trigger: 'blur' }],
  password: [{ validator: validatePassWord, trigger: 'blur' }]
})
</script>

<style lang="scss" scoped>
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: 'cover';
  .login_form {
    .btn {
      width: 100%;
    }
    padding: 30px;
    width: 70%;
    margin-top: 30vh;
    border: 1px solid rgb(28, 51, 180);
    background-size: 'cover';
    background: url('@/assets/images/login_form.png') no-repeat;
    h1 {
      font-size: 30px;
      color: white;
      margin: 20px 0 20px 0;
    }
  }
}
</style>